/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LocalizarFuncionario.java
 *
 * Created on 27/04/2011, 01:05:50
 */
package localizarEditar;

import Classe.BD;
import Classe.Validacao;
import editar.EditarFuncionario;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.MaskFormatter;

/**
 *
 * @author gladson
 */
public class LocalizarFuncionario extends Validacao {

    /** Creates new form LocalizarFuncionario */
    public LocalizarFuncionario() {
        initComponents();
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        StxtOpcLocFuncionario.setEnabled(false);
    }
    String SelecaoFuncionario;

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        cbSelecaoFuncionario = new javax.swing.JComboBox();
        StxtOpcLocFuncionario = new javax.swing.JFormattedTextField();
        jButtonPesquisarFuncionario = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        TabelaFuncionario = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        itxtCodFun = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        jPanel3.setBackground(new java.awt.Color(255, 255, 255));
        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Localizar Funcionário", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 1, 12))); // NOI18N

        jLabel1.setFont(new java.awt.Font("Arial", 1, 11));
        jLabel1.setText("Localizar Por:");

        cbSelecaoFuncionario.setModel(new javax.swing.DefaultComboBoxModel(new String[] {
            "",
            "Data Cadastro",
            "Nome",
            "CPF",
            "RG",
            "Cargo" }));
cbSelecaoFuncionario.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
cbSelecaoFuncionario.addItemListener(new java.awt.event.ItemListener() {
    public void itemStateChanged(java.awt.event.ItemEvent evt) {
        cbSelecaoFuncionarioItemStateChanged(evt);
    }
    });

    StxtOpcLocFuncionario.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

    jButtonPesquisarFuncionario.setText("Pesquisar");
    jButtonPesquisarFuncionario.setToolTipText("Pesquisar");
    jButtonPesquisarFuncionario.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED), javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)));
    jButtonPesquisarFuncionario.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButtonPesquisarFuncionarioActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
    jPanel3.setLayout(jPanel3Layout);
    jPanel3Layout.setHorizontalGroup(
        jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel3Layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                .addComponent(StxtOpcLocFuncionario, javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
                    .addComponent(jLabel1)
                    .addGap(18, 18, 18)
                    .addComponent(cbSelecaoFuncionario, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)))
            .addGap(18, 18, 18)
            .addComponent(jButtonPesquisarFuncionario)
            .addContainerGap(401, Short.MAX_VALUE))
    );
    jPanel3Layout.setVerticalGroup(
        jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel3Layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel1)
                .addComponent(cbSelecaoFuncionario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(StxtOpcLocFuncionario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jButtonPesquisarFuncionario))
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    TabelaFuncionario.setModel(new javax.swing.table.DefaultTableModel(
        new Object [][] {

        },
        new String [] {
            "Data Cadastro", "Código", "Nome", "CPF", "RG", "Data Nascimento", "Cargo"
        }
    ) {
        Class[] types = new Class [] {
            java.lang.String.class, java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
        };

        public Class getColumnClass(int columnIndex) {
            return types [columnIndex];
        }
    });
    jScrollPane1.setViewportView(TabelaFuncionario);

    jButton1.setText("Sair");
    jButton1.setToolTipText("Sair");
    jButton1.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED), javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)));
    jButton1.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
        }
    });

    jPanel4.setBackground(new java.awt.Color(255, 255, 255));
    jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

    jLabel2.setFont(new java.awt.Font("Arial", 1, 11));
    jLabel2.setText("Código Funcionário");

    itxtCodFun.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createEtchedBorder(), javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)));

    jButton2.setText("Editar Funcionário");
    jButton2.setToolTipText("Editar Funcionário");
    jButton2.setBorder(javax.swing.BorderFactory.createCompoundBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED), javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED)));
    jButton2.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton2ActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
    jPanel4.setLayout(jPanel4Layout);
    jPanel4Layout.setHorizontalGroup(
        jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel4Layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jLabel2)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(itxtCodFun, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(18, 18, 18)
            .addComponent(jButton2)
            .addContainerGap(424, Short.MAX_VALUE))
    );
    jPanel4Layout.setVerticalGroup(
        jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(itxtCodFun, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2))
                .addComponent(jLabel2))
            .addContainerGap())
    );

    javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
    jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(
        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 748, Short.MAX_VALUE)
                .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addContainerGap())
    );
    jPanel2Layout.setVerticalGroup(
        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel2Layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(18, 18, 18)
            .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(18, 18, 18)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 307, Short.MAX_VALUE)
            .addGap(18, 18, 18)
            .addComponent(jButton1)
            .addContainerGap())
    );

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addContainerGap())
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addContainerGap())
    );

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );

    java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
    setBounds((screenSize.width-808)/2, (screenSize.height-627)/2, 808, 627);
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonPesquisarFuncionarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonPesquisarFuncionarioActionPerformed
        if (cbSelecaoFuncionario.getSelectedItem().equals("")) {
            JOptionPane.showMessageDialog(rootPane, "Selecione uma opção para consulta!!");
        } else {
            if (validarLocalizador(StxtOpcLocFuncionario.getText())) {
                try {

                    String Opcao = ((String) cbSelecaoFuncionario.getSelectedItem());
                    boolean esc = false;

                    if (Opcao.equalsIgnoreCase("Data Cadastro")) {
                        SelecaoFuncionario = "DATA_CADAS_FUNCIONARIO LIKE '%";
                        esc = true;
                    }
                    if (Opcao.equalsIgnoreCase("Nome")) {
                        SelecaoFuncionario = "NOME_FUNCIONARIO LIKE '%";
                        esc = true;
                    }
                    if (Opcao.equalsIgnoreCase("CPF")) {
                        SelecaoFuncionario = "CPF_FUNCIONARIO";
                        esc = false;
                    }
                    if (Opcao.equalsIgnoreCase("RG")) {
                        SelecaoFuncionario = "RG_FUNCIONARIO LIKE '%";
                        esc = true;
                    }
                    if (Opcao.equalsIgnoreCase("Cargo")) {
                        SelecaoFuncionario = "CARGO_FUNCIONARIO LIKE '%";
                        esc = true;
                    }

                    String sql = "SELECT * FROM FUNCIONARIO WHERE " + SelecaoFuncionario + "='" + StxtOpcLocFuncionario.getText().trim().toUpperCase() + "'";

                    if (esc) {

                        String sq12 = "SELECT * FROM FUNCIONARIO WHERE " + SelecaoFuncionario + "" + StxtOpcLocFuncionario.getText().trim().toUpperCase() + "%'";
                        sql = sq12;
                    }

                    BD banco = new BD();
                    ResultSet res = banco.consultar(sql);

                    DefaultTableModel model = (DefaultTableModel) TabelaFuncionario.getModel();
                    model.setNumRows(0);
                    while (res.next()) {
//---------------------------------formatando a data de cadastro---------------------------

                        String data2 = res.getString("DATA_CADAS_FUNCIONARIO");
                        String dtFinalC = "";
                        if (data2 != null) {
                            String dataC = data2.split(" ")[0];  //corta a strin onde tem o espaço
                            String[] vDataC = dataC.split("-");//corta a onde tem "-"
                            String diaC = vDataC[2];
                            String mesC = vDataC[1];
                            String anoC = vDataC[0];
                            dtFinalC = diaC + "/" + mesC + "/" + anoC;
                        }

//---------------------------------fim da formatação da data--------------------------------

                        String data1 = res.getString("DATA_NASC_FUNCIONARIO");
                        String dtFinal = "";
                        if (data1 != null) {
                            String data = data1.split(" ")[0];  //corta a strin onde tem o espaço
                            String[] vData = data.split("-");//corta a onde tem "-"
                            String dia = vData[2];
                            String mes = vData[1];
                            String ano = vData[0];
                            dtFinal = dia + "/" + mes + "/" + ano;
                        }

                        Object[] linha = {
                            dtFinalC,
                            res.getInt("ID_FUNCIONARIO"),
                            res.getString("NOME_FUNCIONARIO"),
                            res.getString("CPF_FUNCIONARIO"),
                            res.getString("RG_FUNCIONARIO"),
                            dtFinal,
                            res.getString("CARGO_FUNCIONARIO")};
                        model.addRow(linha);
                    }


                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }//GEN-LAST:event_jButtonPesquisarFuncionarioActionPerformed
    MaskFormatter mascara = new MaskFormatter();
    private void cbSelecaoFuncionarioItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbSelecaoFuncionarioItemStateChanged

        StxtOpcLocFuncionario.setEnabled(true);
        if (cbSelecaoFuncionario.getSelectedItem() == "CPF") {
            try {
                mascara.setMask("###.###.###-##");
                mascara.setPlaceholderCharacter(' ');
                mascara.install(StxtOpcLocFuncionario);
            } catch (ParseException ex) {
                Logger.getLogger(LocalizarFuncionario.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else if (cbSelecaoFuncionario.getSelectedItem() == "Data Cadastro") {
            try {
                mascara.setMask("##/##/##");
                mascara.setPlaceholderCharacter(' ');
                mascara.install(StxtOpcLocFuncionario);
            } catch (ParseException ex) {
                Logger.getLogger(LocalizarFuncionario.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {

            mascara.uninstall();
            StxtOpcLocFuncionario.setText("");

        }
    }//GEN-LAST:event_cbSelecaoFuncionarioItemStateChanged

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        if (validarNumero(itxtCodFun.getText())) {

            try {
                int idfun = Integer.parseInt(itxtCodFun.getText().trim());
                String sql = "SELECT * FROM FUNCIONARIO WHERE ID_FUNCIONARIO=" + idfun;
                BD bd = new BD();
                ResultSet r1 = bd.consultar(sql);
                if (r1.next()) {
                    EditarFuncionario edfun = new EditarFuncionario();
                    edfun.iniciar(idfun);
                    edfun.setVisible(true);
                    this.dispose();
                } else {
                    JOptionPane.showMessageDialog(rootPane, "Funcionário não localizado na base de dados!!!");
                    itxtCodFun.setText("");
                }
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(LocalizarFuncionario.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(LocalizarFuncionario.class.getName()).log(Level.SEVERE, null, ex);
            }



        }

    }//GEN-LAST:event_jButton2ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new LocalizarFuncionario().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JFormattedTextField StxtOpcLocFuncionario;
    private javax.swing.JTable TabelaFuncionario;
    private javax.swing.JComboBox cbSelecaoFuncionario;
    private javax.swing.JTextField itxtCodFun;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButtonPesquisarFuncionario;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
